<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/dtree.css" media="all" />
    <link rel="stylesheet" href="/resources/layui_ext/dtree/font/dtreefont.css" media="all" />
</head>
<body class="childrenBody">
<!-- 查询条件开始-->

<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form" >
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="uname" id="search_uname" class="layui-input searchVal" placeholder="请输入姓名" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" id="search_username" class="layui-input searchVal" placeholder="请输入用户名" />
                    </div>
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn layui-btn-radius" lay-submit lay-filter="doSearch" shiro:hasPermission="user:view"><span class="layui-icon layui-icon-search"></span>查询</button>
                    <button type="reset" class="layui-btn layui-btn-warm layui-btn-radius"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
                </div>
            </div>
        </form>
    </blockquote>
</form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="userTable" lay-filter="userTable"></table>
    <!-- 工具栏 -->
    <div id="userToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-radius layui-btn-sm" shiro:hasPermission="user:create"><span class="layui-icon layui-icon-add-1"></span>添加用户</button>
    </div>

    <div id="userRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm layui-btn-radius" shiro:hasPermission="user:update"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger layui-btn-radius" shiro:hasPermission="user:delete">
            <span class="layui-icon layui-icon-delete"></span>删除
        </button>
        <button type="button" lay-event="resetPwd" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-warm" shiro:hasPermission="user:resetPwd">
            <span class="layui-icon layui-icon-refresh"></span>重置密码
        </button>
        <button type="button" lay-event="selectUserRole" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal" shiro:hasPermission="user:selectRole">
            <span class="layui-icon layui-icon-about"></span>分配角色
        </button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div  style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form class="layui-form" style="width:80%;margin-top: 20px;" id="dataFrm" lay-filter="dataFrm">
        <!-- 隐藏域，保存角色id -->
        <input type="hidden" name="uid" id="uid">


        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="username" id="username" lay-verify="required" placeholder="请输入用户名">
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="uname" id="uname" lay-verify="required" placeholder="请输入姓名">
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="uphone"  lay-verify="required|phone" placeholder="请输入手机号">
            </div>
        </div>
        <div class="layui-form-item layui-row layui-col-xs12">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="uemail" lay-verify="email" placeholder="请输入邮箱">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="usex" value="1" title="男" checked="">
                    <input type="radio" name="usex" value="0" title="女" >
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">是否可用</label>
                <div class="layui-input-block">
                    <input type="radio" name="available" value="1" title="是" checked="">
                    <input type="radio" name="available" value="0" title="否" >
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn layui-btn-radius" lay-submit lay-filter="doSubmit"><span class="layui-icon layui-icon-add-1"></span>提交</button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-radius"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->

<!-- 用户分配角色弹出层  开始 -->
<div style="display: none;padding: 5px" id="selectUserRoleDiv">
    <table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
</div>
<!-- 用户分配角色弹出层  结束 -->


<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.extend({
        dtree: '/resources/layui_ext/dtree/dtree'   // {/}的意思即代表采用自有路径，即不跟随 base 路径
    }).use(['jquery','form','table','layer','dtree'],function () {
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var dtree=layui.dtree;

        //渲染表格
        var tableIns = table.render({
            elem: '#userTable'//绑定表格组件的id
            , toolbar: '#userToolBar'
            , height: 'full-110'
            , title: '用户数据表'
            , url: '/user/userList/' //数据接口
            , page: true //开启分页
            , cols: [ [
                {type: 'checkbox', fixed: 'left'}
                , {field: 'uid', title: 'ID', align: 'center', width: '5%'}
                , {field: 'username', title: '用户名', align: 'center', width: '9%'}
                , {field: 'uname', title: '姓名', align: 'center', width: '8%'}
                , {field: 'usex', title: '性别', align: 'center',templet:function (d) {
                        return d.available == 1 ? "<font color='green'>男</font>" :"<font color='#663399'>女</font>";
                    },width: '5%'}
                , {field: 'uphone', title: '联系手机', align: 'center', width: '9%'}
                , {field: 'uemail', title: '联系邮箱', align: 'center', width: '13%'}
                ,{field:'available', title:'是否可用',align:'center',templet:function (d) {
                        return d.available == 1 ? "<font color='blue'>是</font>" :"<font color='red'>否</font>";
                    }, width: '7%'}
                , {field: 'ucreatetime', title: '创建时间', align: 'center', width: '13%'}
                , {field: 'type', title: '操作类型', align: 'center', width: '28%',toolbar: "#userRowBar"}
            ] ]
            //回调函数
            , done: function (res,curr, count) {
                if (curr > 1 && res.data.length == 0) {
                    var pageValue = curr -1;
                    tableIns.reload({
                        page:{curr:pageValue}
                    })
                }
            }

        });

        //绑定模糊查询监听事件 submit是layui规定的 doSearch是查询按钮绑定的属性 自己定义的
        form.on("submit(doSearch)", function (data) {
            //表格数据重新加载
            tableIns.reload({
                where: data.field //额外的参数
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //关闭跳转页面
        });

        //监听头工具栏
        table.on("toolbar(userTable)",function (obj) {
                switch (obj.event) {
                    case "add":
                        openAdduserWindow();
                        break;
                }
        });

        var mainIndex;//弹出层的索引下标
        var url;//提交路径

        //弹出添加框函数
        function openAdduserWindow() {
            mainIndex = layer.open({
                type:1,
                title:"添加用户信息",
                area:["600px","450px"],
                content:$("#addOrUpdateDiv"),//内容窗口
                success:function () {
                    //每次开启清空弹出框数据
                    $("#dataFrm")[0].reset();
                    url="/user/adduser";
                }
            });
        }

        var checkFlag = false;
        //验证用户名是否重复
        $("#username").bind("blur",function () {
            //获取用户名
            var username =$(this).val().trim();
            $.post("/user/checkUserName",{"username":username},function (result) {
                if (result.exist){
                    checkFlag =true;//登陆名存在
                }else {
                    checkFlag =false;
                }
                layer.msg(result.message)
            },"json")
        });

        //监听添加和修改窗口的提交事件
        form.on("submit(doSubmit)", function (data) {
            //表格数据重新加载
            console.log(data);
            if (!checkFlag) {
                $.post(url, data.field, function (result) {
                    //判断是否成功
                    if (result.status) {
                        //成功表格刷新
                        tableIns.reload();
                    }
                    //弹出是否成功消息
                    layer.msg(result.message);
                    //关闭窗口
                    layer.close(mainIndex)
                }, "json");
                return false; //关闭跳转页面
            }else {
                layer.msg("用户名已存在，请重新输入！")
            }
        });

        //行工具栏
        table.on("tool(userTable)", function (obj) {
            var data =obj.data;
            switch (obj.event) {
                case 'update':
                    openUpdateUserWindow(data);
                    break;
                case 'delete':
                    deleteUser(data);
                    break;
                case 'resetPwd':
                    resetPassWord(data);
                    break;
                case 'selectUserRole':
                    selectUserRole(data);
                    break;
            }
        });

        //修改
        function openUpdateUserWindow(data) {
            mainIndex = layer.open({
                type:1,
                title:"修改用户",
                area:["600px","450px"],
                content:$("#addOrUpdateDiv"),//内容窗口
                success:function () {
                    form.val("dataFrm",data);
                    url="/user/updateuser";
                    //设置只读
                    $("#username").attr("readonly","readonly");
                    //
                    $("#username").unbind("blur");
                }
            });
        }

        //删除单条数据
        function deleteUser(data) {
            layer.confirm('是否要删除这条数据吗?', {icon: 3, title: '提示'}, function (index) {
                $.post("/user/deleteOne", {"id": data.uid}, function (result) {
                    if (result.status) {
                        //删除成功后刷新数据表格
                        tableIns.reload();
                    }
                    layer.msg(result.message);
                }, "json");
                layer.close(index);//关闭提示框
            });
        }

        //重置密码
        function resetPassWord(data) {
            layer.confirm('是否要重置密码吗?', {icon: 3, title: '提示'}, function (index) {
                $.post("/user/resetPwd", {"id": data.uid}, function (result) {
                    if (result.status) {
                        //删除成功后刷新数据表格
                        tableIns.reload();
                    }
                    layer.msg(result.message);
                }, "json");
                layer.close(index);//关闭提示框
            });
        }

        //分配角色
        function selectUserRole(data){
            mainIndex = layer.open({
                type:1,
                title:"分配"+data.uname+"的角色",
                area:["500px","400px"],
                content:$("#selectUserRoleDiv"),//内容窗口
                btn: ['确定', '取消'],
                btnAlign: 'c',
                yes: function (index, layero) {
                    //获取所有选中节点
                    var checkData = table.checkStatus('roleTable');//获取选中行的数据
                    if (checkData.data.length==0){
                        layer.msg("未选中")
                    }else{
                        console.log(checkData.data)
                        var idArr =[];
                        for (var i=0;i<checkData.data.length;i++){
                            idArr.push(checkData.data[i].roleid);
                        }
                        var ids = idArr.join(",");
                        $.post("/user/saveUserRole",{"roleids":ids,"userid":data.uid},function (result) {
                            layer.msg(result.message)
                        },"json");
                        layer.close(index);//关闭提示框
                    }
                },
                btn2: function (index, layero) {
                },
                success:function () {
                    initRoleTable(data);
                }
            });
        }


         //初始化角色列表
        function initRoleTable(data){
            table.render({
                elem: '#roleTable'//绑定表格组件的id
                , url: '/user/initRoleByUserId/' //数据接口
                ,where:{
                    "id":data.uid
                }
                , cols: [ [
                    {type: 'checkbox', fixed: 'center'}
                    , {field: 'roleid', title: 'ID', align: 'center'}
                    , {field: 'rolename', title: '角色名称', align: 'center'}
                    , {field: 'remark', title: '角色备注', align: 'center'}
                ] ]
            });
        }
    });
</script>
</body>
</html>